package com.weimob.mcloud.wechatwork.customer.es;

import com.alibaba.fastjson.JSON;
import com.weimob.mcloud.wechatwork.common.utils.LogUtils;
import com.weimob.mcloud.wechatwork.customer.es.dto.CustomerLoseFinalTouchBiDocument;
import com.weimob.mcloud.wechatwork.customer.es.dto.CustomerLoseFinalTouchBiEO;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class CustomerLoseFinalTouchSearch {
    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Resource
    private RestHighLevelClient client;

    private static final Logger LOGGER = LoggerFactory.getLogger(CustomerLoseFinalTouchSearch.class);

    /**
     * 根据dto 分页查询
     *
     * @param eo       查询参数dto
     * @param pageable spring data es 分页封装类 页码开始从0开始
     * @see PageRequest
     **/
    public Page<CustomerLoseFinalTouchBiEO> queryByPage(PageRequest pageable, CustomerLoseFinalTouchBiEO eo) {
        QueryBuilder queryBuilder = paramsBuild(eo);
        Query query = new NativeSearchQueryBuilder().withQuery(queryBuilder)
                .withPageable(pageable)
                .build();
        query.setTrackTotalHits(Boolean.TRUE);
        LogUtils.info(LOGGER, "CustomerGroupStatisticBiDocument, queryByPage,request:{}", JSON.toJSONString(eo));
        SearchHits<CustomerLoseFinalTouchBiDocument> searchHits =
                elasticsearchRestTemplate.search(query, CustomerLoseFinalTouchBiDocument.class);

        return resultBuild(pageable, searchHits);
    }


    private QueryBuilder paramsBuild(CustomerLoseFinalTouchBiEO eo) {

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        if (!StringUtils.isEmpty(eo.getPrivateSphereId())) {
            boolQueryBuilder.must(QueryBuilders.termQuery("privateSphereId", eo.getPrivateSphereId()));
        }

        if (null != eo.getCustomerId()) {
            boolQueryBuilder.must(QueryBuilders.termQuery("customerId", eo.getCustomerId()));
        }

        if (null != (eo.getOrgUserId())) {
            boolQueryBuilder.must(QueryBuilders.termQuery("orgUserId", eo.getOrgUserId()));
        }

        if (null != eo.getTouchType()) {
            boolQueryBuilder.must(QueryBuilders.termQuery("touchType", eo.getTouchType()));
        }

        if (null != eo.getTaskId()) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("taskId", eo.getTaskId()));
        }

        if (null != eo.getEndTouchTime()){
            boolQueryBuilder.must(QueryBuilders.rangeQuery("touchTime").lte(eo.getEndTouchTime().getTime()));
        }
        return boolQueryBuilder;
    }

    private Page<CustomerLoseFinalTouchBiEO> resultBuild(PageRequest pageable, SearchHits<CustomerLoseFinalTouchBiDocument> searchHits) {
        if (null == searchHits || !searchHits.hasSearchHits()) {
            return new PageImpl<>(new ArrayList<>(), pageable, 0);
        }

        List<CustomerLoseFinalTouchBiEO> content = new ArrayList<>(searchHits.getSearchHits().size());
        for (SearchHit<CustomerLoseFinalTouchBiDocument> hit : searchHits) {
            CustomerLoseFinalTouchBiDocument customerGroupStatisticBiDocument = hit.getContent();
            content.add(customerGroupStatisticBiDocument.toEO());
        }
        return new PageImpl<>(content, pageable, searchHits.getTotalHits());
    }

}